package com.diamond.facade;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;

import com.diamond.service.IService;

public class CommonFacade<T> {
	public IService<T> service;

	public void setService(IService<T> service) {
		this.service = service;
	}
	
	public boolean deleteByParam(Serializable[] args) throws Exception {
		for (int i = 0; i < args.length; i++) {
//			service.deleteByParam(args);
		}
		return true;
	}
	
	public void save(T entity){
		try {
			service.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void update(T entity){
		try {
			service.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void deleteByKey(Serializable id){
		try {
			service.deleteByKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public Object findById(Serializable id){
		try {
			return service.findById(id);
		} catch (Exception e) {
			return null;
		}
		
	}

	public List<T> findByCriteria(int firstResult, int maxResults,DetachedCriteria criteria) throws Exception {
		return service.findByCriteria(maxResults, (firstResult - 1)* maxResults, criteria);
	}

	public Long getRowCount(String HQL) throws Exception {
		return service.getRowCount(HQL);
	}
	
	public List<T> findByHQL(int currPage, int pagesize, String hql) {
		return service.findByHQL(currPage, pagesize, hql);
	}
	
	public Map findPage(int currPage, int pagesize, String hql) {
		return service.findPage(currPage, pagesize, hql);
	}
	
	public Map findCountAndPageBySql(int currPage, int pagesize, String sql,Class<T> cls) {
		return service.findCountAndPageBySql(currPage,pagesize,sql,cls);
	}
	
	public List<T> findByHQL(final String hql) {
		return service.findByHQL(hql);
	}
	
	public List<T> find(final String hql,Object[] values) {
		try {
			return service.find(hql,values);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
